/*
 * @Author: zxc
 * @Date: 2021-01-23 19:16:42
 * @LastEditTime: 2021-01-24 10:15:45
 * @LastEditors: zxc
 * @Description: 函数组合
 */
import _ from "lodash"

const compose = (f, g) => {
  return x => f(g(x))
}

// const test = compose(_.first, _.reverse)
// console.log(test(["xxx", "ssss"]))

/**
 * @Author: zxc
 * @Date: 2021-01-23 19:23:34
 * @func lodash中的组合函数
 */
// const LodashCompseTest = _.flowRight(_.toUpper, _.first, _.reverse)
// console.log(LodashCompseTest(["xxx", "acdgs"]))

/**
 * @Author: zxc
 * @Date: 2021-01-24 09:46:24
 * @func 实现一个compose
 */
const myCompose = (...args) => v => args.reverse().reduce((acc, fn) => fn(acc), v)


// const myCompseTest = myCompose(_.toUpper, _.first, _.reverse)
// const myCompseTest = myCompose(myCompose(_.toUpper, _.first), _.reverse)
// const myCompseTest = myCompose(_.toUpper, myCompose(_.first, _.reverse))
// console.log(myCompseTest(["xxx", "acdgs"]))


/**
 * @Author: zxc
 * @Date: 2021-01-24 10:01:34
 * @func 调试compose
 * @description NEVER SAY DIE => never-say-die
 */
const logTrace = _.curry((tag, v) => {
  console.log(tag, v);
  return v
})
const split = _.curry((sep, str) => _.split(str, sep))
const join = _.curry((sep, arr) => _.join(arr, sep))
const map = _.curry((fn, arr) => _.map(arr, fn))
const debugCompose = myCompose(join('-'), logTrace('map'), map(_.toLower), logTrace('split'), split(' '))

// console.log(debugCompose('NEVER SAY DIE'));